﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using RaisingForce.Raise.Language.Rules;

namespace RaisingForce.Raise.Language.RStates
{
    sealed public class PreviousChain :
        Internal.ChainContainerState
    {
        #region Constructors

        private PreviousChain(string name)
            : base(name)
        { }

        #endregion

        #region Private Static Declarations

        private static readonly
            Internal.ChainContainerStateCollection<PreviousChain> chainStates =
            new Internal.ChainContainerStateCollection<PreviousChain>();

        private static readonly PreviousChain noneState =
            new PreviousChain(SharedChainData.NoneChainName);

        static PreviousChain()
        {
            foreach (string allowedChainName in SharedChainData.AllowedChainNames)
            {
                PreviousChain chainState = new PreviousChain(allowedChainName);
                PreviousChain.chainStates.Add(chainState);
            }
        }

        #endregion

        #region Properties

        public bool IsNone
        {
            get
            {
                return object.ReferenceEquals(this, PreviousChain.noneState);
            }
        }

        #endregion

        #region Operators

        public static explicit operator PreviousChain(CurrentChain currentChain)
        {
            Contract.Requires(currentChain != null);
            Contract.Ensures(Contract.Result<PreviousChain>() != null);
            return PreviousChain.FromCurrentChain(currentChain);
        }

        public static explicit operator CurrentChain(PreviousChain previousChain)
        {
            if (previousChain == null)
            {
                throw new ArgumentNullException("previousChain");
            }
            Contract.Ensures(Contract.Result<CurrentChain>() != null);
            Contract.EndContractBlock();
            return previousChain.ToCurrentChain();
        }

        #endregion

        #region Public Methods

        [Pure]
        public CurrentChain ToCurrentChain()
        {
            if (this.IsNone)
            {
                throw new InvalidOperationException();
            }
            Contract.Ensures(Contract.Result<CurrentChain>() != null);
            Contract.EndContractBlock();
            return CurrentChain.Get(this.Name);
        }

        #endregion

        #region Base Overrides

        protected override bool isDefault
        {
            get
            {
                return object.ReferenceEquals(this, PreviousChain.Default);
            }
        }

        #endregion

        #region Public Static Declarations

        public static PreviousChain None
        {
            get
            {
                Contract.Ensures(Contract.Result<PreviousChain>() != null);
                return PreviousChain.noneState;
            }
        }

        public static PreviousChain Default
        {
            get
            {
                Contract.Ensures(Contract.Result<PreviousChain>() != null);
                return PreviousChain.noneState;
            }
        }

        [Pure]
        public static PreviousChain Get(string name)
        {
            if (!ChainRules.NameIsValid(name))
            {
                throw new ArgumentException("name");
            }
            Contract.Ensures(Contract.Result<PreviousChain>() != null);
            Contract.EndContractBlock();
            return PreviousChain.chainStates[name];
        }

        [Pure]
        public static IEnumerator<PreviousChain> GetEnumerator()
        {
            Contract.Ensures(Contract.Result<IEnumerator<PreviousChain>>() != null);
            return PreviousChain.chainStates.GetEnumerator();
        }

        [Pure]
        public static IEnumerable<PreviousChain> GetEnumerable()
        {
            Contract.Ensures(Contract.Result<IEnumerable<PreviousChain>>() != null);
            return PreviousChain.chainStates.GetEnumerable();
        }

        [Pure]
        public static PreviousChain FromCurrentChain(CurrentChain currentChain)
        {
            if (currentChain == null)
            {
                throw new ArgumentNullException("currentChain");
            }
            Contract.Ensures(Contract.Result<PreviousChain>() != null);
            Contract.EndContractBlock();
            return PreviousChain.Get(currentChain.Name);
        }

        #endregion
    }
}
